package com.zht.view;

import com.zht.handle.LoginHandler;
import sun.rmi.log.LogHandler;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class LoginView  extends JFrame {
    /*声明，组件

    * */
    SpringLayout springLayout= new SpringLayout();
    JPanel centerJPanel = new JPanel(springLayout);// 用弹簧布局面板

    JLabel titleLabel = new JLabel("学生管理系统",JLabel.CENTER); //让JLabel 的对齐方式变为汇总
    JLabel userNameLabel = new JLabel("账号：");
    JLabel pwdLabel = new JLabel("密码：");
    JTextField nameField = new JTextField();
    JPasswordField pwdField =  new JPasswordField();

    JButton restBtn = new JButton("重置");
    JButton loginBtn = new JButton("登录");


    public JTextField getNameField() {
        return nameField;
    }

    public void setNameField(JTextField nameField) {
        this.nameField = nameField;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

    public void setPwdField(JPasswordField pwdField) {
        this.pwdField = pwdField;
    }

    public LoginView()  {
        super("登录界面");
        Font titleFont = new Font("华文行楷", Font.PLAIN, 40);
        titleLabel.setPreferredSize(new Dimension(0,80));
        titleLabel.setFont(titleFont);

        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(centerFont);
        pwdLabel.setFont(centerFont);
        nameField.setFont(centerFont);
        pwdLabel.setFont(centerFont);
        restBtn.setFont(centerFont);
        loginBtn.setFont(centerFont);

        nameField.setPreferredSize(new Dimension(200,30));
        pwdField.setPreferredSize(new Dimension(200,30));

        Container contentPane = getContentPane();
        // 把元素加入到容器
        contentPane.setLayout(new BorderLayout());

        /*把元素加入到面板中*/

        /* 监听 按钮点击的时候，触发的时刻，要执行的任务*/

        LoginHandler logHandler = new LoginHandler(this);
        loginBtn.addActionListener(logHandler); // 按钮只要被点击了，就执行了，logHandler 的actionPerformed 方法

        centerJPanel.add(userNameLabel);
        centerJPanel.add(nameField);


        centerJPanel.add(pwdLabel);
        centerJPanel.add(pwdField);
        centerJPanel.add(loginBtn);
        centerJPanel.add(restBtn);

        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel),Spring.width(nameField)),Spring.constant(20));
        int offsetX = childWidth.getValue() / 2;
        springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offsetX,SpringLayout.HORIZONTAL_CENTER,centerJPanel);
        springLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerJPanel);
        /* nameField*/
        springLayout.putConstraint(SpringLayout.WEST,nameField,20,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,nameField,0,SpringLayout.NORTH,userNameLabel);

        /* pwdLabel*/
        springLayout.putConstraint(SpringLayout.EAST,pwdLabel,0,SpringLayout.EAST,userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH,pwdLabel,20,SpringLayout.SOUTH,userNameLabel);
        /*pwdField*/
        springLayout.putConstraint(SpringLayout.WEST,pwdField,20,SpringLayout.EAST,pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH,pwdField,0,SpringLayout.NORTH,pwdLabel);
        /* loginBtn*/
        springLayout.putConstraint(SpringLayout.WEST,loginBtn,50,SpringLayout.WEST,pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH,loginBtn,20,SpringLayout.SOUTH,pwdLabel);

        springLayout.putConstraint(SpringLayout.WEST,restBtn,50,SpringLayout.EAST,loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH,restBtn,0,SpringLayout.NORTH,loginBtn);


        contentPane.add(titleLabel,BorderLayout.NORTH); // 放到北边，
        contentPane.add(centerJPanel,BorderLayout.CENTER); // 面板放中间



        URL resource = LoginView.class.getClassLoader().getResource("1.png");
        setIconImage(new ImageIcon(resource).getImage());

        setSize(600,400);
        setLocationRelativeTo(null);
        // 关闭退出程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 大小不可以改变
        setResizable(false);
        setVisible(true);
    }
}
